home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / UTILITIE / UNIX_COH / 2774A.ZIP / TOOLS1.SHZ / TOOLS1 / getutent.c < prev    next >
C/C++ Source or Header  |  1991-07-03  |  1KB  |  74 lines

  1. /**
  2.  **  Simulate SysV getutent(3) calls, as best as can be done in
  3.  **  a System III environment, at least.
  4.  **
  5.  **  Author: Paul Sutcliffe, Jr.  <paul@devon.uucp>
  6.  **
  7.  **  I hereby place this in the public domain.
  8.  **  No warranties expressed or implied.
  9.  **/
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <utmp.h>
  15.  
  16. static char *utmpfil = "/etc/utmp";    /* default utmp file */
  17. static FILE *ufp = NULL;        /* file pointer to utmp file */
  18.                     /* NULL = no utmp file open  */
  19. static struct utmp ut;            /* buffer for utmp record */
  20.  
  21. struct utmp *getutent(), *getutline();
  22. void pututline(), setutent(), endutent(), utmpname();
  23.  
  24. struct utmp *
  25. getutent()
  26. {
  27.     FILE *fopen();
  28.  
  29.     if (ufp == NULL) {
  30.         if ((ufp = fopen(utmpfil, "r+")) == NULL) {
  31.             return((struct utmp *)NULL);
  32.         }
  33.     }
  34.     do {
  35.         if (fread((char *)&ut, sizeof(ut), 1, ufp) != 1) {
  36.             return((struct utmp *)NULL);
  37.         }
  38.     } while (ut.ut_name[0] == 0);        /* valid entry? */
  39.  
  40.     return(&ut);
  41. }
  42.  
  43. struct utmp *
  44. getutline(line)
  45. struct utmp *line;
  46. {
  47.     do {
  48.         if (strcmp(ut.ut_line, line->ut_line) == 0) {
  49.             return(&ut);
  50.         }
  51.     } while (getutent() != (struct utmp *)NULL);
  52.  
  53.     return((struct utmp *)NULL);
  54. }
  55.  
  56. void
  57. setutent()
  58. {
  59.     if (ufp != NULL) rewind(ufp);
  60. }
  61.     
  62. void
  63. endutent()
  64. {
  65.     if (ufp != NULL) fclose(ufp);
  66. }
  67.  
  68. void
  69. utmpname(file)
  70. char *file;
  71. {
  72.     utmpfil = file;
  73. }
  74.